<?php
/**
 * Created by PhpStorm.
 * User: lhuan
 * Date: 2019/11/7
 * Time: 20:08
 */
namespace app\index\controller;

use app\index\model\Article;
use app\index\model\ArticleClassify;
use app\index\model\ExpenditureBills;
use app\index\model\SysUser;
use think\Controller;

class Bills extends Index
{
    public function index()
    {
        $model = new ExpenditureBills();
        $model->map = [
            'status' => ['neq','0'],
        ];
        foreach(input('param.search/a',[]) as $k => $v){
            if($v){
                if($k == 'income_or_disburse'){
                    $model->map[$k] = $v == '其它' ? '': $v;
                }else{
                    $model->map[$k] = ['like', '%'.$v.'%'];
                }
            }
        }
//        print_r($model->map);die;
        $model->limit = input('param.limit',15);
        $model->p = input('param.p',1);
        $res = $model->getList();
        $total = $model->count();
        return ajax_return( 200 , 'successful' , [
            'list' => $res,
            'total' => $total
        ] );
    }

    /**
     * 月账单
     * */
    public function getMonthBills()
    {
        $year = input('param.year',date('Y'));
        $month = input('param.month',date('m'));
        $day = date('t', strtotime($year . '-' .$month));

        $data = [];
        $amount = 0;

        $date = [];
        $dayStatistics = [];

        for($i = 0;$i < $day;$i++)
        {
            $d = $i+1;

            $model = new ExpenditureBills();

            $model->map['bill_create_time'] = [
                'between' , ["{$year}-{$month}-{$d} 00:00:00", "{$year}-{$month}-{$d} 23:59:59"]
            ];
            $model->map['income_or_disburse'] = '支出';

            $result = $model->getAll();

            $amountMoneyTotal = 0;

            foreach($result as $item)
            {
                $amountMoneyTotal += ( $item->amount_money - $item->refund );
            }

            $amount += $amountMoneyTotal;
            $amountMoneyTotal = $amountMoneyTotal / 100;


            $data[] = [
                'total' => $amountMoneyTotal,
                'bills' => $result,
                'month' => $month,
                'day' => $d
            ];
            $date[] = $d;
            $dayStatistics[] = $amountMoneyTotal;
        }

        return ajax_return( 200 , 'successful' , [
            'list' => $data,
            'total' => $day,
            'amount' => $amount,
            'chartsData' => [
                'date' => $date,
                'dayStatistics' => $dayStatistics
            ]
        ] );



    }

    /**
     * 年账单
     * */
    public function getYearBills()
    {
        $year = input('param.year',date('Y'));

        $data = [];
        $date = [];
        $amount = 0;

        $monthStatistics = [];

        for($month = 1; $month<=12; $month++)
        {
            $day = date('t', strtotime($year . '-' .$month));

            $model = new ExpenditureBills();

            $model->map['bill_create_time'] = [
                'between' , ["{$year}-{$month}-01 00:00:00", "{$year}-{$month}-{$day} 23:59:59"]
            ];
            $model->map['income_or_disburse'] = '支出';

            $result = $model->getAll();

            $amountMoneyTotal = 0;

            foreach($result as $item)
            {
                $amountMoneyTotal += $item->amount_money;
            }

            $amount += $amountMoneyTotal;
            $amountMoneyTotal = $amountMoneyTotal / 100;

            $data[] = [
                'total' => $amountMoneyTotal,
                'bills' => $result,
                'month' => $month
            ];
            $date[] = $month.'月';
            $monthStatistics[] = $amountMoneyTotal;

        }


        return ajax_return( 200 , 'successful' , [
            'list' => $data,
            'total' => $month-1,
            'amount' => $amount,
            'chartsData' => [
                'date' => $date,
                'monthStatistics' => $monthStatistics
            ]
        ]);
    }

    /* *
     * 消费排名
     * */
    public function getPaymentRank()
    {
        $type = input('type', 'month'); // month | year | all

        $model = new ExpenditureBills();


        return ajax_return( 200 , 'successful' , [
            'year' => $model->getPaymentRank('year'),
            'month' => $model->getPaymentRank('month')
        ]);
    }

}